<script>
	import Vue from 'vue'
	export default {
		methods:{
			//初始化字典数据，在storage里
			async initDictionary() {
			    let res = await this.$api.getDictionary();
				uni.setStorageSync("dictionary", res.data.data);
			},
			//初始化服务类别数据，在storage里
			async initCatagory() {
			    let res = await this.$api.getCategories();
				let data = res.data.data
				if(res.data.status == 200){
					const cata = []
					data.forEach(e => {
						if(e.categoryList){
							e.categoryList.forEach(n => {
								delete n.categoryList
								cata.push(n)
							})
							delete e.categoryList
							cata.push(e)
						}else{
							delete e.categoryList
							cata.push(e)
						}
					})
					uni.setStorageSync("categorys", cata);
				}
			},
			initData(){
				this.initDictionary()
				this.initCatagory()
			},
			//检查token与userinfo是否存在
			checkToken(){
				let token = uni.getStorageSync('token')
				let userInfo = uni.getStorageSync("userInfo")
				if(!token){
					let that = this
					uni.login({
						success(loginRes) {
							that.$api.getSessionKey({code: loginRes.code}).then((loginValue)=>{
								that.token = loginValue.data.data
								uni.setStorageSync('token', loginValue.data.data)
								//初始化数据
								that.initData()
								// 获取token成功后判断是否有微信userinfo，没有跳转
								if(!userInfo.nickName){
									uni.navigateTo({
										url: '/pages/user/user'
									})
								}
							}).catch((err)=>{
								console.log('request fail', err);
							})
						}, 
					})
				}else{
					//token存在，初始化数据
					this.initData()
					if(!userInfo){
						// 有token 但是没有用户数据
						uni.navigateTo({
							url: '/pages/user/user'
						})
					}
				}
			}
		},
		onLaunch: function() {
			console.log('App Launch')
			uni.getSystemInfo({
				success: function(e) {
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
				}
			})
		},
		onShow: function() {
			let that = this
			that.checkToken()
			// uni.getSetting({
			//    success(res) {
			// 	   
			// 		   if (res.authSetting['scope.userInfo']) {
			// 				// 拥有用户信息授权
			// 				//检查token与 userinfo
			// 		   }
			// 		  if (res.authSetting['scope.userInfo'] === undefined) {
			// 			  console.log('未授权用户信息,跳转登陆页面')
			// 			  uni.navigateTo({
			// 				url: '/pages/user/user'
			// 			  })
			// 		  }
			// 		  if (res.authSetting['scope.userLocation'] === undefined) {
			// 			  console.log('未授权位置信息')
			// 		  }
			//    },
			//    fail(err) {
			//    	console.log('getSetting err--', err)
			//    }
			// })
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	@import "colorui/main.css";
	@import "colorui/icon.css";

</style>
